<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var str = 'asdgsde';
        String.prototype.sub = function (a, b) {
            var str1 = '';
            a = a == undefined ? 0 : a;
            b = b == undefined ? this.length : b;

            for (i = a; i < this.length; i++) {
                if (i == b) {
                    return str1
                } else {
                    str1 += this[i];
                }
            }
            return str1
        }
        console.log(str.sub(2, 4))

    </script>


    <script>
        // 用函数封装一个字符串的substr方法
        // 字符串截取 截取从n 开始m 个字符
        var str = 'asdgsdetrtwyr';
        var a = 2;
        var b = 5;
        function substr(string, n, m) {
            if (!m) m = string.length - n;
            var str1 = "";
            for (let i = n; i < n + m; i++) {
                str1 += str[i]

            }
            return str1; 
        }
        var num = substr(str, 2,6);
        console.log(num);



        

    </script>

</body>

</html>